<?php
namespace app\common\model;
use think\Model;
use think\Validate;
use app\common\tool\Upload;
class GoodsDetails extends Model
{
	protected $table = 'goods_details';
	protected $pk = 'goods_details_id';
	protected $autoWriteTimestamp = true;
	protected $createTime = 'goods_details_create_time';
	protected $updateTime = 'goods_details_update_time';
	// 验证规则
	protected $rule = [
	    ['goods_details_name|模板规则名称', ['require']],
		['goods_details_content|模板规则内容', ['require']],
	]; 
	
	// 验证场景
	protected $scene = [
        'insert' => ['goods_details_name', 'goods_details_content'],
        'update' => ['goods_details_name', 'goods_details_content'],
    ];

    // 验证器
	public function intValidate(){
        $validate = new \think\Validate($this->rule);
        foreach ($this->scene as $k1=>$v1)
            $validate->scene($k1, $v1);
        return $validate;
    }
 
    public static function refresh()
    {
        $merchant_id = session('merchant.merchant_id');
        $data = GoodsDetails::field('goods_details_id,goods_details_name')->where('goods_details_merchant_id', $merchant_id)->select();
        return $data;
    }  
    // 用户列表的需要商户的id
    public static function showlist($pagesize = 2)
    {
        $merchant_id = session('merchant')['merchant_id'];
    	$data = GoodsDetails::field('goods_details_id,goods_details_name,goods_details_content,goods_details_create_time')
    	->where([
    		'goods_details_merchant_id' =>$merchant_id,
    		'goods_details_is_delete' => 0
     	])
    	->order('goods_details_id', 'desc')->paginate(2);
     	return $data;
    }

    protected static function init()
	{
		GoodsDetails::beforeInsert(function ($goodsDetails) 
		{
			$merchant_id = session('merchant')['merchant_id'];
			$goodsDetails['goods_details_merchant_id'] = $merchant_id;
		});
	}


	public function changeDel($id)
	{
        $merchant_id = session('merchant')['merchant_id'];
		$result = GoodsDetails::where([
            'goods_details_id' => $id,
            'goods_details_merchant_id' => $merchant_id,
        ])->setField('goods_details_is_delete', 1); 
	   if($result)
       {
            return true;
       }else
       {
            return false;
       }
    }



    public static function getDetail($id)
    {
        $merchant_id = session('merchant')['merchant_id'];
        $details = GoodsDetails::where([
            'goods_details_id' => $id,
            'goods_details_merchant_id' => $merchant_id,
        ])->find();

        return $details;
    }


    public static function edit($id, $detail_name, $content)
    {
        $merchant_id = session('merchant')['merchant_id'];
        $detail = new GoodsDetails;
        $result = $detail->save([
            'goods_details_name' => $detail_name,
            'goods_details_content' => $content 
            ],[
            'goods_details_id' => $id,
            'goods_details_merchant_id' => $merchant_id
            ]);

        
        if(!$result)
            return false;

        return true;
    } 


    // 运营平台商品详情模板列表
    public static function totalGoodsDetail($pagesize)
    {
        $where['goods_details_is_delete'] = ['eq', 0];
        $keyword = input('param.keyword');
        if($keyword) 
        {
            $where['goods_details_name'] = ['like', "%$keyword%"];
        }

        $data = GoodsDetails::field('goods_details_id,goods_details_name')
                ->order('goods_details_id', 'desc')
                ->where($where)
                ->paginate($pagesize);

       return  $data;
    } 


    public static function changeDel2($id)
    {
        $result = GoodsDetails::where('goods_details_id', $id)->setField('goods_details_is_delete', 1);

        if(!$result)
            return false;
        return true;
    }
    
    
    public static function getGoodsDetails($id)
    {
        $data = GoodsDetails::field('goods_details_id,goods_details_name,goods_details_content')->where('goods_details_is_delete', 0)->find($id);
        return $data;
    }  
}